home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ddj0897.zip / RCSC.ZIP / LIB51 / AVAIL.C < prev    next >
C/C++ Source or Header  |  1997-01-12  |  1KB  |  47 lines

  1. /*
  2. ** Allocate n bytes of (possibly zeroed) memory.
  3. ** Entry: n = Size of the items in bytes.
  4. **    clear = "true" if clearing is desired.
  5. ** Returns the address of the allocated block of memory
  6. ** or NULL if the requested amount of space is not available.
  7. */
  8. extern char * _memptr;
  9. extern char * _memend;
  10.  
  11. _alloc(n, clear) unsigned n, clear; {
  12.   char *oldptr;
  13.   if(n < avail(YES)) {
  14.    /* need to disable interrupt to provide exclusive access
  15.     in multitasking environment */
  16.     disable();
  17.     oldptr = _memptr;
  18.     _memptr += n;
  19.     enable();
  20.     if(clear) pad(oldptr, NULL, n);
  21.     return (oldptr);
  22.     }
  23.   return (NULL);
  24.   }
  25.  
  26.  
  27. /*
  28. ** Return the number of bytes of available memory.
  29. ** In case of a stack overflow condition, if 'abort'
  30. ** is non-zero the program aborts with an 'S' clue,
  31. ** otherwise zero is returned.
  32. */
  33. avail(abort) int abort; {
  34. char *ptr;
  35.    /* need to disable interrupt to provide exclusive access
  36.     in multitasking environment */
  37.   disable();
  38.   if(_memend < _memptr)
  39.     ptr = NULL;
  40.   else
  41.     ptr = (_memend - _memptr + 1);
  42.   enable();
  43.  
  44.   return ptr;
  45.   }
  46.  
  47.